package com.txh.blog.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.txh.blog.bean.*;
import com.txh.blog.service.BlogService;
import com.txh.blog.service.TagsService;
import com.txh.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class BlogController{

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagsService tagsService;

    @RequestMapping("/blogs")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1") int page,
                                @RequestParam(name = "pageSize",required = true,defaultValue = "4")int pageSize){
        ModelAndView mv=new ModelAndView();
        List<Blog> all = blogService.findAll(page, pageSize);
        List<Type> types = typeService.listType();
        PageInfo<Blog> info=new PageInfo<>(all);
        mv.addObject("pageInfo",info);
        mv.addObject("types",types);
        mv.setViewName("admin/blogs");
        return mv;
    }
    @RequestMapping("/blogs/search")
    public ModelAndView search(@RequestParam(name = "page",required = true,defaultValue = "1") int page,
                               @RequestParam(name = "pageSize",required = true,defaultValue = "4")int pageSize,
                               SearchBlog searchBlog, Model model){
        ModelAndView mv=new ModelAndView();
        blogService.transformRecommend(searchBlog);
        List<BlogList> search = blogService.search(page, pageSize, searchBlog);
        List<Type> types = typeService.listType();
        PageInfo<BlogList> pageInfo = new PageInfo<>(search);
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("types",types);
        mv.setViewName("admin/blogs");
        return mv;
    }
    @RequestMapping("/blogs/input")
    public ModelAndView input(){
        List<Type> types = typeService.listType();
        List<Tags> tags = tagsService.listTags();
        ModelAndView mv=new ModelAndView();
        mv.addObject("types",types);
        mv.addObject("tags",tags);
        mv.addObject("blog",new Blog());
        mv.setViewName("admin/blogs-input");
        return mv;
    }
    @PostMapping("/blogs/save")
    @ResponseBody
    public String saveBlog(Blog blog, HttpSession session){
        blog.setType(typeService.findById(blog.getTypeId()));
        blog.setTags(tagsService.listTag(blog.getTagIds()));
         blogService.saveBlog(blog);
         return "0";//表示保存成功
    }
    @RequestMapping("/blogs/update")
    public ModelAndView update(@RequestParam Long id){
        Blog blog = blogService.getBlog(id);
        List<Type> types = typeService.listType();
        List<Tags> tags = tagsService.listTags();
        ModelAndView mv=new ModelAndView();
        mv.addObject("types",types);
        mv.addObject("tags",tags);
        mv.addObject("blogs",blog);
        mv.setViewName("admin/blogs-update");
        return mv;
    }
    @PostMapping("/blogs/edit")
    @ResponseBody
    public String edit(Blog blog, RedirectAttributes redirectAttributes){
        blogService.updateBlog(blog);
        return "0";
    }
    @RequestMapping("/blogs/delete")
    public String deleteBlog(@RequestParam Long id){
       blogService.deleteBlog(id);
       return "redirect:/admin/blogs";
    }
}
